import org.junit.Test;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Errorhandling2 {

	int iWait;
	
	@Test
	public void qS1() throws Exception{
		iWait = 3000;
		//Thread.sleep(iWait);
		String[][] xTC, xTS;
		String vKW, vXpath, vTestData;
		FirefoxDriver myD = new FirefoxDriver();
	
		
		String xPath = "C:/Priya/Silverback 4.4/Automation Test Cases/QService.xls";
		
		//Read the entire TC and TS
		xTC = xlreadwrite.xlRead(xPath,"TestCases");
		xTS = xlreadwrite.xlRead(xPath, "TestSteps");
		
		
		System.out.println("Test Step sheet is" + xTS);
		
		//Go to each row in TC and identify which TC to execute
		
		for (int i = 1; i<xTC.length; i++) {   //Go to each row in TC
			System.out.println("TCID  is " + xTC[i][0]);
			//Thread.sleep(iWait);
			if (xTC[i][3].equalsIgnoreCase("Y")) {
				Thread.sleep(iWait);
				System.out.println("Yes, execute this");
				Thread.sleep(iWait);
			
			//go to every test step if it is y
			for (int j=1; j<xTS.length; j++){
				//Thread.sleep(iWait);
				System.out.println("TSID is" +xTS[i][0]);
				Thread.sleep(iWait);
				if (xTC[i][0].equalsIgnoreCase(xTS[j][1])){
					//Thread.sleep(iWait);
					System.out.println("KW to execute is " + xTS[j][4]);
					Thread.sleep(iWait);
					System.out.println("xpath to execute is " + xTS[j][5]);
					System.out.println("Data to execute is " + xTS[j][6]);
					
					
					// call the corresponding KW function
					vKW = xTS[j][4] ;
					vXpath = xTS[j][5];
					vTestData = xTS[j][6];
					String vError = "Pass"; // This means Before the try is executed it is pass and if it correctly executed all through try, vError will be pass
					
		try{
						if (vKW.equalsIgnoreCase("enterText")) {
							KW.enterText(myD, vXpath, vTestData);
						}
							if (vKW.equalsIgnoreCase("navigateBrowser")){
							KW.navigateBrowser(myD, vTestData);
							}
								if(vKW.equalsIgnoreCase("clickElement")){
								KW.clickElement(myD, vXpath);
								}
									if (vKW.equalsIgnoreCase("verifyText")){
										vError = KW.verifyText(myD, vXpath, vTestData);
										System.out.println("Test is a" + vError );
									}
									if (vKW.equalsIgnoreCase("readText")){
										vError = KW.readText(myD, vXpath);
										System.out.println("Test is a" + vError);
									}
									if(vKW.equalsIgnoreCase("waitTime")){
										KW.waitTime(3000);
										}
						if (vError.equalsIgnoreCase("Pass")) {
						System.out.println("Pass:" + xTC[i][0] + "_" + vKW); //If it's come this far then pass
						xTS[j][8] ="Pass";
						} else {
							System.out.println("Fail:" + xTC[i][0] + "_" + vKW); //If it's come this far then pass
							xTS[j][8] ="Error happened as a function returned fail";
						}
						
					} catch(Exception myError) {
						System.out.println("Fail:" + xTC[i][0] + "_" + vKW); //Fail if the try loop  fails.
						System.out.println("Error Happened" + myError);  //prints the errors into the logs.
						xTS[j][9] ="Error Happened" + myError;
					}
					
					
						}
						
					}
				}
			}
		myD.quit();
		
		//Write back the test Steps with the error
		xlreadwrite.writeXL("C:/Priya/Silverback 4.4/Results/TestResults.xls", "TestSteps Results", xTS );
	}
		

}

//try{
//	}
//	catch(Exception e){
//		System.out.println("Error Happened" +e);
//   }
			
//Some times the error may occur if we are not waiting for the page to load. So use wait statements. 